package org.jeecg.modules.tls.rsmg.rsfile.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.tls.rsmg.rsfile.entity.RsFile;
import org.jeecg.modules.tls.rsmg.rsfile.service.IRsFileService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: rs_file
 * @Author: jeecg-boot
 * @Date:   2022-07-23
 * @Version: V1.0
 */
@Api(tags="rs_file")
@RestController
@RequestMapping("/rsfile/rsFile")
@Slf4j
public class RsFileController extends JeecgController<RsFile, IRsFileService> {
	@Autowired
	private IRsFileService rsFileService;
	
	/**
	 * 分页列表查询
	 *
	 * @param rsFile
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "rs_file-分页列表查询")
	@ApiOperation(value="rs_file-分页列表查询", notes="rs_file-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<RsFile>> queryPageList(RsFile rsFile,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {

		QueryWrapper<RsFile> queryWrapper = QueryGenerator.initQueryWrapper(rsFile, req.getParameterMap());
		Page<RsFile> page = new Page<RsFile>(pageNo, pageSize);
		IPage<RsFile> pageList = rsFileService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @ApiOperation(value="rs_folder-分页列表查询", notes="rs_folder-分页列表查询")
	 @GetMapping(value = "/filelist")
	 public Result queryList(@RequestParam(required = false) String id,@RequestParam(required = false) String fileType,@RequestParam(required = false) String name,HttpServletRequest req) {
		 System.out.println("fileType:"+fileType);
		 System.out.println("========"+req.getHeader("tenant-id")+"========");
		 String  tenantId = req.getHeader("tenant-id");
		 return Result.OK(rsFileService.getFileAndFolder(id,fileType,name));
	 }
	/**
	 *   添加
	 *
	 * @param
	 * @return
	 */
	@AutoLog(value = "rs_file-添加")
	@ApiOperation(value="rs_file-添加", notes="rs_file-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestParam MultipartFile file,@RequestParam String folderId,@RequestParam String userId,@RequestParam String remark){
		if(rsFileService.uploadFile(file,folderId,userId,remark)){
			return Result.ok();
		}else {
			return Result.error(500,"文件上传错误");
		}
	}
	
	/**
	 *  编辑
	 *
	 * @param rsFile
	 * @return
	 */
	@AutoLog(value = "rs_file-编辑")
	@ApiOperation(value="rs_file-编辑", notes="rs_file-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RsFile rsFile) {
		rsFileService.updateById(rsFile);
		return Result.OK("编辑成功!");
	}


	 /**
	  *  重命名
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "rs_file-重命名")
	 @ApiOperation(value="rs_file-编辑", notes="rs_file-编辑")
	 @RequestMapping(value = "/rename", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> renameFile(@RequestParam String id,@RequestParam String name) {
		 if(rsFileService.reNameFile(id,name)){
			 return Result.ok();
		 }else {
			 return Result.error("重命名失败");
		 }
	 }

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "rs_file-通过id删除")
	@ApiOperation(value="rs_file-通过id删除", notes="rs_file-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		if(rsFileService.deleteFile(id)){
			return Result.ok();
		}else {
			return Result.error(500,"删除失败");
		}
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "rs_file-批量删除")
	@ApiOperation(value="rs_file-批量删除", notes="rs_file-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.rsFileService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "rs_file-通过id查询")
	@ApiOperation(value="rs_file-通过id查询", notes="rs_file-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<RsFile> queryById(@RequestParam(name="id",required=true) String id) {
		RsFile rsFile = rsFileService.getById(id);
		if(rsFile==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(rsFile);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param rsFile
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RsFile rsFile) {
        return super.exportXls(request, rsFile, RsFile.class, "rs_file");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RsFile.class);
    }

	@PostMapping(value = "uploadImg")
	 public Result<String> uploadImg(@RequestParam MultipartFile file){
		if(rsFileService.uploadImg(file)!=null){
			return Result.ok(rsFileService.uploadImg(file));
		}else {
			return Result.error(500,"上传失败");
		}
	}
}
